from chainlit.utils import mount_chainlit
from fastapi.responses import JSONResponse
from requests import Request
import chainlit as cl
from app import create_app
from app.common.result import Result
from app.exception.biz_exception import BizException

# 加载上传文件的配置

# 项目初始化
app = create_app()


# 业务异常处理
@app.exception_handler(BizException)
async def biz_exception_handler(request: Request, exc: BizException):
    """
    业务异常处理
    :param request:参数必须有，否则匹配不到
    :param exc: 业务异常
    :return: JSONResponse
    """
    result = Result.fail(message=exc.msg, code=exc.code)
    return JSONResponse(content=result.model_dump())


@app.exception_handler(Exception)
async def exception_handler(request: Request, exc: Exception):
    """
    全局异常处理
    :param request:参数必须有，否则匹配不到
    :param exc: 全局异常
    :return: JSONResponse
    """
    result = Result.fail(message=str(exc), code=-1)
    # 转换为字典
    return JSONResponse(content=result.model_dump())




# 挂载chainlit当作fastAPI的子应用
mount_chainlit(app=app, target="./app/chainlit-ui/chainlit_app.py", path="/")